﻿
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Internal.PreviousInput;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using XS.Tunnel.Cad.Cmd.Data;
using XS.Tunnel.Cad.SteelBracing;
using XS.Tunnel.Cad.SteelBracing.Component;
using XS.Tunnel.Cad.SteelBracing.Component.SteelShotcrete;
using XS.Tunnel.Cad.SteelBracing.Component.SteelShotcrete.GrilleComponent;
using XS.Tunnel.Cad.SteelBracing.Component.SteelShotcrete.UniversalBeamComponent;
using XS.Tunnel.Cad.SteelBracing.Infrastructure;
using XS.Tunnel.Cad.SteelBracing.Infrastructure.Extensions;

namespace XS.Tunnel.Cad.Cmd
{


   
    public class SteelBracingCommand
    {
        [Autodesk.AutoCAD.Runtime.CommandMethod("test2")]
        public void Test()
        {
            try
            {
                // 宝塔  七心圆  大拐角
                var d1 = 型钢数据("308739130211173637", "417539010706015493", type: ColumnArchFootType.LargeCornerJoin);
                var d2 = 型钢数据("306995278794196229", "329129307811087621", 200, 0);  //秦岭   单心圆
                var d3 = 型钢数据("306995278794196229", "330476630101198085", 400, 0);  //秦岭   七星圆
                var d4 = 型钢数据("306995278794196229", "329130019206989061", 600, 0);  //秦岭   三星圆

                Draw(d1);
                Draw(d2);
                Draw(d3);
                Draw(d4);



                //var d5 = 型钢数据("306995278794196229", "429405797093475589", 600, 0, ColumnArchFootType.YuanShunJoin,1);  
                //Draw(d5);


            }
            catch (Exception ex)
            {

                throw;
            }

        }



        [Autodesk.AutoCAD.Runtime.CommandMethod("test")]
        public void Test2()
        {

            var d1 = 格栅("308739130211173637", "417539010706015493",grille: RectangleGrille()); //type: ColumnArchFootType.LargeCornerJoin    // 宝塔  七心圆  大拐角
            var d2 = 格栅("306995278794196229", "329129307811087621",200, 0,grille: RectangleGrille());  //秦岭   单心圆
            var d3 = 格栅("306995278794196229", "330476630101198085",400,0, grille: GetFigureOfEightGrille());  // 八字节  秦岭   七星圆
            var d4 = 格栅("306995278794196229", "329130019206989061",600, 0, grille: RectangleGrille());  //秦岭   三星圆


          

            绘制格栅(d1);
            绘制格栅(d2);
            绘制格栅(d3);
            绘制格栅(d4);

        }




        [Autodesk.AutoCAD.Runtime.CommandMethod("test3")]
        public void Test3()
        {



        }



        public void Draw(TestData d)
        {
            var component = new UniversalBeamBuild(d.DarkHoleInfo, d.Points, d.Param);
            component.Build();
            component.Draw();
        }

        public void 绘制格栅(TestData d)
        {
            try {
                var component = new GrilleBuild(d.DarkHoleInfo, d.Points, d.Param);
                component.Build();
                component.Draw();
            }
            catch (FriendlyException ex)
            {
                Application.ShowAlertDialog(ex.Message); 
            }
        }


        public Grille RectangleGrille(double num = 10) {

            return new RectangleGrille
            {
                URebar = new URebar {
                     Height =25/ num,
                     Width = 30/ num,
                },
                 HorizontalTopLength=9/ num,   //9   //15
                 HorizontalBottomLength = 4.5 / num,
                 Height = 25 / num,
                 SidelingHorizontalLength=25 / num,
                 Stirrup=new Stirrup
                 {
                    Height = 30/ num,
                    Interval=35/num
                 }
            };
        }

        public Grille GetFigureOfEightGrille(double num = 10)
        {
            double interval = 40;

            return new FigureOfEightGrille
            {
                URebar = new URebar
                {
                    Height = 25 / num,  
                    Width = 30 / num,
                },
                Height = 25 / num,
                Interval=5/num,
                Width= (interval-4) / num,
                Stirrup = new Stirrup
                {
                    Height = 30 / num,
                    Interval = interval / num
                }
            };
        }



        public TestData 型钢数据(string pid,string id, double x=0,double y = 0, ColumnArchFootType type= ColumnArchFootType.ExtendBluntJoin, double num = 10)
        {

            if (string.IsNullOrEmpty(pid))
            {
                pid = "306995278794196229";
            }
            var darkinfo =GetDark();
            var param = GetParam(num);
            darkinfo.ColumnArchFootType = type;
            var list = GetPoints(pid,id,x,y,num);

            var ss= list.ToJson();

            return new TestData(darkinfo, list, param);
        }

        public TestData 格栅(string pid, string id, double x = 0, double y = 0, Grille grille=null, ColumnArchFootType type = ColumnArchFootType.ExtendBluntJoin, double num = 10)
        {

            if (string.IsNullOrEmpty(pid))
            {
                pid = "306995278794196229";
            }
            var darkinfo = GetDark();
            var param = GetGrilleParam(num, grille);
            darkinfo.ColumnArchFootType = type;
            var list = GetPoints(pid, id, x, y, num);
            return new TestData(darkinfo, list, param);
        }


        public List<ISysPoint> GetPoints(string pid, string id, double x = 0, double y = 0, double num = 10)
        {
            var rep = new Cmd.Data.TunnelRepository();
            rep.MajorPrjectId = pid;
            var data = rep.GetDarkHolePoints(id, "liningSection"); 
            var list = new List<ISysPoint>();

            foreach (var item in data)
            {
                ISysPoint m = null;
                if (item.lineType == 1)
                {
                    m = new ArcPoint(item.abscissa.Value / num + x, item.ordinate.Value / num + y, item.circleRadius.Value / num, item.startAngle.Value, item.endAngle.Value, item.totalAngle.Value);
                }
                else
                {
                    m = new LinePoint(item.abscissa.Value / num + x, item.ordinate.Value / num + y, item.point2X.Value / num + x, item.point2Y.Value / num + y);
                }
                m.No = item.arcSort;
                m.BizTpyeCode = item.componentBizTpyeCode;
                m.Name = item.arcNameText;
                m.Code = item.arcNameCode;
                list.Add(m);
            }
            return list;
        }

        /// <summary>
        /// 缩放比例
        /// </summary>
        /// <param name="num">缩小倍数</param>
        /// <returns></returns>
        public GrilleParameter GetGrilleParam(double num, Grille grille)
        {
            return new GrilleParameter
            {
                 
                 Height=30/ num,
                 Scope= SteelShotcreteScope.Arch, 
                 Subsection=3, 
                 WallInterval=4/ num,
                 AngleIron=new AngleIron { 
                   Color= "#ADFF2F",// 线的颜色
                   Height=40/num,
                   Length=20/num,
                   Thickness=2/num  //  规格里面的是mm  这里需要的cm
                 },
                 WallFoot=new SteelPlateParameter { 
                     
                     Color= "#FF00FF",
                     Count=1,
                     Length=40/num,
                     Thickness=10/num,
                     Weight=20/num,
                 },
                  Grille= grille,
                  
                  

            };
        
        }


        public UniversalBeamParam GetParam(double num)
        {
            var param = new UniversalBeamParam
            {
                // 分段
                Subsection = 3,
                WallInterval = 8 / num,
                UniversalBeamHeight = 20/ num,  //mm  这里需要cm

                // 钢板 中间的
                Middle = new SteelPlateParameter
                {
                    Count = 2,  // 钢板数量
                    Length = 2.4 / num, // 长度
                    Thickness =0.14 / num, // 厚度0.139
                    Weight = 40 / num  // 宽， 目前没用
                },
                // 侧方钢板
                InvertedJoinWallFoot = new SteelPlateParameter
                {
                    Count = 1,
                    Length = 6 / num,
                    Thickness = 0.14 / num,
                    Weight = 1.4 / num
                },
                // 拱墙下面的钢板
                WallFoot = new SteelPlateParameter
                {
                    Count = 1,
                    Length = 2.4 / num,
                    Thickness =0.14 / num,
                    Weight =2 / num
                },
                Scope = SteelShotcreteScope.FullCrossSection, // 范围
                InvertedArchSubsection = 3, //仰拱分段
            };
            return param;
        }

        public DarkHoleInfo GetDark()
        {
            var darkinfo = new DarkHoleInfo
            {
                Id = "000001",
                ArchFootType = RoundCornerTypeEnum.Round,
                CircleNumber = CircleCenterTypeEnum.SingleHeart,
                ColumnArchFootType = ColumnArchFootType.ExtendBluntJoin,// ColumnArchFootType.LargeCornerJoin, // 大墙脚
                ColumnCircleNumber = CircleCenterTypeEnum.SingleHeart,

                LiningCrossSectionType = LiningCrossSectionTypeEnum.ConstantSection,

                IsSetInvertedArch = true,//
              
            };

            return darkinfo;
        }




        protected void Drawing(Action<BlockTableRecord, Transaction> action)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            // 开启事务
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //打开块表
                var bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                //打开块表记录
                var btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                action(btr, trans);
                //事务提交
                trans.Commit();
            }
        }

        protected void Drawing(List<Entity> entities)
        {
            Drawing((btr, trans) =>
            {
                foreach (var item in entities)
                {
                    btr.AppendEntity(item);
                    trans.AddNewlyCreatedDBObject(item, true);
                }
            });
        }

        protected void Drawing(params Entity[] entities)
        {
            Drawing(entities.ToList());
        }

        protected void Drawing(params ISysPoint[] entities)
        {
            Drawing(entities.ToEntitys());
        }




    }

    public class TestData
    {
        public TestData(DarkHoleInfo darkinfo, List<ISysPoint> list, SteelShotcreteParameter param)
        {
            DarkHoleInfo = darkinfo;
            Points = list;
            Param = param;
        }
        public DarkHoleInfo DarkHoleInfo { get; set; }
        public List<ISysPoint> Points { get; set; }
        public SteelShotcreteParameter Param { get; set; }

    }


}
